package com.wuyou.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.wuyou.config.AlipayConfig;
import com.wuyou.dto.OrderDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * description 广告支付宝支付接口
 * author 刘雷雨
 * date 2022/9/30 15:54
 * version 1.0
 **/
@RestController
@RequestMapping("advAlipay")
public class AdvAlipayController {

    @Autowired
    private AlipayClient alipayClient;


    /**
     * description 付款接口
     * @param orderDTO = 订单付款信息对象
     * @return
     */
    @PostMapping("alipayTradePagePay")
    public String alipayTradePagePay(@RequestBody OrderDTO orderDTO) {

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ orderDTO.getOrderNum() +"\","
                + "\"total_amount\":\""+ orderDTO.getTotalPrice() +"\","
                + "\"subject\":\""+ orderDTO.getOrderName() +"\","
                + "\"body\":\""+ orderDTO.getOrderDesc() +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求
        String result = null;
        try {
            result = alipayClient.pageExecute(alipayRequest).getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        //输出
        return result;
    }
}
